package com.xiaobu.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaobu.shop.domain.entity.ShopUser;
import com.xiaobu.shop.domain.exception.ShopException;
import com.xiaobu.shop.domain.security.ShopUserDetails;
import com.xiaobu.shop.mapper.ShopUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service
public class ShopUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    ShopUserMapper shopUserMapper;
    @Override
    public UserDetails loadUserByUsername(String usercode) throws UsernameNotFoundException {
            LambdaQueryWrapper<ShopUser> wrapper=new LambdaQueryWrapper();
            wrapper.eq(ShopUser::getUsercode,usercode);
            ShopUser user = shopUserMapper.selectOne(wrapper);
            if (Objects.isNull(user)){
                throw new ShopException("用户信息不存在");
            }
            ShopUserDetails shopUserDetails = new ShopUserDetails();
            shopUserDetails.setUser(user);
            return shopUserDetails;
        }
}
